import os
from typing import Optional
from pydantic_settings import BaseSettings
from pydantic import Field


class Settings(BaseSettings):
    """应用配置"""
    
    # 应用基础配置
    app_name: str = "SmartTrader Pro"
    version: str = "1.0.0"
    debug: bool = Field(default=False, env="DEBUG")
    
    # API配置
    api_v1_prefix: str = "/api/v1"
    secret_key: str = Field(env="SECRET_KEY")
    access_token_expire_minutes: int = 60 * 24 * 7  # 7天
    
    # 数据库配置
    database_url: str = Field(default="sqlite:///./data/smarttrader.db", env="DATABASE_URL")
    
    # PostgreSQL配置(可选)
    postgres_server: Optional[str] = Field(default=None, env="POSTGRES_SERVER")
    postgres_user: Optional[str] = Field(default=None, env="POSTGRES_USER")
    postgres_password: Optional[str] = Field(default=None, env="POSTGRES_PASSWORD")
    postgres_db: Optional[str] = Field(default=None, env="POSTGRES_DB")
    postgres_port: int = Field(default=5432, env="POSTGRES_PORT")
    
    # Redis配置
    redis_url: str = Field(default="redis://localhost:6379", env="REDIS_URL")
    
    # InfluxDB配置
    influx_url: str = Field(default="http://localhost:8086", env="INFLUX_URL")
    influx_token: str = Field(env="INFLUX_TOKEN")
    influx_org: str = Field(env="INFLUX_ORG")
    influx_bucket: str = Field(default="market_data", env="INFLUX_BUCKET")
    
    # 数据源API配置
    tushare_token: Optional[str] = Field(default=None, env="TUSHARE_TOKEN")
    alpha_vantage_key: Optional[str] = Field(default=None, env="ALPHA_VANTAGE_KEY")
    
    # CORS配置
    backend_cors_origins: list[str] = [
        "http://localhost:3000",
        "http://localhost:8000",
        "https://localhost:3000",
        "https://localhost:8000",
    ]
    
    # 任务队列配置
    celery_broker_url: str = Field(default="redis://localhost:6379/1", env="CELERY_BROKER_URL")
    celery_result_backend: str = Field(default="redis://localhost:6379/2", env="CELERY_RESULT_BACKEND")
    
    # 日志配置
    log_level: str = Field(default="INFO", env="LOG_LEVEL")
    
    class Config:
        env_file = ".env"
        case_sensitive = True


# 全局配置实例
settings = Settings()